<template>
  <div class="order-history">
    <el-table :data="orders" style="width: 100%">
      <el-table-column prop="orderId" label="订单号" width="180" />
      <el-table-column prop="shopName" label="店铺" />
      <el-table-column prop="total" label="金额" width="100">
        <template #default="{row}">¥{{ row.total }}</template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template #default="{row}">
          <el-tag :type="statusType(row.status)">
            {{ statusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="下单时间" width="180">
        <template #default="{row}">{{ formatDate(row.createTime) }}</template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="{row}">
          <el-button size="mini" @click="viewDetail(row.orderId)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { formatDate } from '@/utils/date'  // 确保这个导入路径正确

const orders = ref([0])
const total = ref(0)
const pageSize = ref(10)
const currentPage = ref(1)

// 确保这个函数有定义
const statusText = (status) => {
  const statusMap = ['待支付', '已支付', '已完成', '已取消']
  return statusMap[status] || '未知状态'
}

// 确保这个函数有定义
const statusType = (status) => {
  const typeMap = ['warning', 'success', '', 'info']
  return typeMap[status] || ''
}

const viewDetail = (orderId) => {
  console.log('查看订单详情:', orderId)
}

const handlePageChange = (page) => {
  currentPage.value = page
  loadOrders()
}

onMounted(() => {
  loadOrders()
})
</script>

<style scoped>
.pagination {
  margin-top: 20px;
  justify-content: center;
}
</style>
